﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

public partial class views_ucproduct_details : System.Web.UI.UserControl
{
   
    protected void Page_Load(object sender, EventArgs e)
    { 
        if (!IsPostBack)
        {
            if (Request.QueryString["proid"] != null)
            {
               
                this.rpt_sanpham.DataSource = getdata();
                this.rpt_sanpham.DataBind();

            }
        }
    }

    public DataTable getdata()
    {
        //ham nay thay cho ham lay du lieu len tu database
        DataTable data = new DataTable();
        data.Columns.Add("ID");
        data.Columns.Add("Name");
        data.Columns.Add("Price");
        data.Columns.Add("NewPrice");
        data.Columns.Add("Image");
        data.Columns.Add("Descrition");

        DataRow Dr = data.NewRow();

        string strproductid = Request.QueryString["proid"].ToString();
        DataRow[] dr_product = ((DataTable)Cache.Get("products_view")).Select("productid = " + strproductid);
        string strprice = "", stroldprice = "";
        float discount;
        if (dr_product[0].ItemArray[12].ToString().Trim() != "")
        {

            discount = float.Parse(dr_product[0].ItemArray[12].ToString().Trim());
            stroldprice = dr_product[0].ItemArray[10].ToString().Trim();
            strprice = ((int.Parse(dr_product[0].ItemArray[10].ToString().Trim()) * discount) / 100).ToString();

            Dr[2] = stroldprice;
            Dr[3] = strprice;
        }
        else
        {
            Dr[3] = "Giá: " + dr_product[0].ItemArray[10].ToString().Trim();
        }

        Dr[1] = dr_product[0].ItemArray[1].ToString();
        Dr[0] = dr_product[0].ItemArray[0].ToString();
        Dr[5] = get_product_description(strproductid);
        Dr[4] = "~/images/products/" + dr_product[0].ItemArray[6].ToString();

        data.Rows.Add(Dr);
        return data;

    }

    public string get_product_description(string strsql)
    {
        DataProvider datapro = new DataProvider();
        SqlConnection conn = datapro.Connect();
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_product_description", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@productid", strsql);
            dt.Load(cmd.ExecuteReader());
            return dt.Rows[0]["description"].ToString();
        }
        catch { return ""; }
        finally { conn.Close(); }
    }
}